
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import _thread
from datetime import datetime
import time
 

plugin_version = '1.0'
checked_num = 0

def id_Update(self):
    self.send_response(200)
    self.send_header('Content-type', 'application/json')
    self.end_headers()
    self.wfile.write(json.dumps(data).encode())
def read_file(url):
    try:
        with open(url,'r') as f:
            a = f.read()
            jsoned = json.loads(a)
            return jsoned
    except:
        with open(url,'w') as f:
            if url == 'config.json':
                a = f.write(json.dumps({"url":"localhost","port":8888},
                sort_keys=True,
                indent=4,
                separators=(',', ': ')))
                return {"url":"localhost","port":8888}
            else:
                f.write('{}')
                return {}
def load():
    global config,data,check,host
    config = read_file('config.json')
    data = read_file('data.json')
    check = read_file('sgined.json')
    host = (config['url'], config['port'])

def write(fileType):
    if fileType == 'config':
        with open('config.json','w') as f:
            f.write(json.dumps(config,
                sort_keys=True,
                indent=4,
                separators=(',', ': ')))
    elif fileType == 'data':
        with open('data.json','w') as f:
            f.write(json.dumps(data,
                sort_keys=True,
                indent=4,
                separators=(',', ': ')))
    elif fileType == 'check':
        with open('sgined.json','w') as f:
            f.write(json.dumps(check,
                sort_keys=True,
                indent=4,
                separators=(',', ': ')))



 
class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        global checked_num
        if '/Auth?' in self.path:
            plugin_ved = self.path.split('=')[1]
            if plugin_ved == plugin_version:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':0}).encode())
            elif plugin_ved == '':
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':400}).encode())
            else:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':1}).encode())

        elif '/Xuidquery?' in self.path:
            plugin_name = self.path.split('=')[1]
            if plugin_name in data:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':0,'xuid':data[plugin_name]['xuid']}).encode())
            elif plugin_name == '':
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':400}).encode())
            else:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':1}).encode())

        elif '/Register?' in self.path:
            Resister_Data = self.path.split('=')
            xuid = Resister_Data[1].split('&')[0]
            password = Resister_Data[2].split('?')[0]
            name = Resister_Data[3]
            if xuid != '' and password != '' and name != '':
                playerdata = {"password":password,"xuid":xuid}
                data[name] = playerdata
                write('data')
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':0}).encode())
            else:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({'code':400}).encode())
        
        elif '/Login?' in self.path:
            Login_Data = self.path.split('=')
            print(Login_Data)
            xuid = Login_Data[1].split('&')[0]
            password = Login_Data[2].split('?')[0]
            name = Login_Data[3]
            print(xuid,password,name)
            if xuid != '' and password != '' and name != '':
                if name in data:
                    data_xuid = data[name]['xuid']
                    data_password = data[name]['password']
                    if data_xuid == xuid and data_password == password and name not in check:
                        checked_num += 1
                        check[name] = True
                        write('check')
                        self.send_response(200)
                        self.send_header('Content-type', 'application/json')
                        self.end_headers()
                        self.wfile.write(json.dumps({"total":checked_num,"code":0,"items":[{"itemname":"Clock","count":2}]}).encode())
                    elif name in check:
                        self.send_response(200)
                        self.send_header('Content-type', 'application/json')
                        self.end_headers()
                        self.wfile.write(json.dumps({"code":2}).encode())
                    else:
                        self.send_response(200)
                        self.send_header('Content-type', 'application/json')
                        self.end_headers()
                        self.wfile.write(json.dumps({"code":1}).encode())
        
        elif '/Check?' in self.path:
            Player = self.path.split('=')[1]
            if Player not in check:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({"code":0}).encode())
            else:
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({"code":1}).encode())
            

def server():
    print("Starting server, listen at: %s:%s" % host)
    server = HTTPServer(host, Resquest)
    server.serve_forever()
    


def clean_check():
    print('Time reset Sgin Success!')
    while True:
        timea = datetime.now().strftime("%H:%M")
        if timea == '00:00':
            check = {}
            write('check')
            print('Reset Success')
        time.sleep(1)

if __name__ == '__main__':
    load()
    _thread.start_new_thread(server,())
    _thread.start_new_thread(clean_check,())
    while 1:
        pass

